data structures two pointers

Please click on ads to support us..

Python Code:

t = int(input())

bEle = [None for _ in range(10**6+1)]
for _ in range(t):
    n,m,k = map(int,input().split())

    a = [int(x) for x in input().split()]
    b = [int(x) for x in input().split()]

    for i in range(m):
        if bEle[b[i]]==None:
            bEle[b[i]] = 1
        else:
            bEle[b[i]] += 1
    
    count = 0
    ans = 0
    for i in range(m):
        if bEle[a[i]]!=None:
            bEle[a[i]] -=1
            if bEle[a[i]]>=0:
                count+=1
    
    if count>=k:
        ans+=1
        for i in range(m,n):
        if bEle[a[i-m]]!=None:
            bEle[a[i-m]] += 1
            if bEle[a[i-m]]>0:
                count-=1
        if bEle[a[i]]!=None:
            bEle[a[i]]-=1
            if bEle[a[i]]>=0:
                count+=1
        if count>=k:
            ans+=1
    
    for i in range(m):
        bEle[b[i]] = None
    
    print(ans)


Comments

Submit
0 Comments
More Questions

1475A - Odd Divisor
1454B - Unique Bid Auction
978C - Letters
501B - Misha and Changing Handles
1496A - Split it
1666L - Labyrinth
1294B - Collecting Packages
1642B - Power Walking
1424M - Ancient Language
600C - Make Palindrome
1669D - Colorful Stamp
1669B - Triple
1669A - Division
1669H - Maximal AND
1669E - 2-Letter Strings
483A - Counterexample
3C - Tic-tac-toe
1669F - Eating Candies
1323B - Count Subrectangles
991C - Candies
1463A - Dungeon
1671D - Insert a Progression
1671A - String Building
1671B - Consecutive Points Segment
1671C - Dolce Vita
1669G - Fall Down
4D - Mysterious Present
1316B - String Modification
1204A - BowWow and the Timetable
508B - Anton and currency you all know